<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="../../css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../js/api.js"></script>
<script type="text/javascript" src="../../js/axios.js"></script>
<script type="text/javascript" src="../../js/jquery.js"></script>
</head>
<body>
<div class="place"> <span>位置：</span>
  <ul class="placeul">
    <li>采购管理</li>
    <li>品牌管理</li>
    <li>添加</li>
  </ul>
</div>
<div class="formbody">
  <div class="formtitle"><span>品牌信息</span></div>
  <form id="form">
  <ul class="forminfo">
    <li>
      <label>品牌名称</label>
      <input name="brandName" type="text" class="dfinput" id="brandName"/>
      <i class="tips">必填，不能超过50个字符</i>
    </li>
    <li>
      <label>状态</label>
      <select class="dfselect" name="brandStatus">
          <option value="1">可用</option>
          <option value="2">不可用</option>
        </select>
      <i>必选</i>
    </li>
    <li>
      <label>创建人</label>
      <input name="createId" type="hidden" id="createId" value="101"/>
      <input name="createName" id="createName" type="text" value="马云" readonly="readonly" class="roinput" />
      <i>不能编辑</i>
    </li>
    <li>
      <label>创建时间</label> <!--value="2017-11-18 15:36:10" readonly="readonly"-->
      <input name="createTime" id="createTime" class="roinput" readonly="readonly"/>
      <i>不能编辑</i>
    </li>
    <li>
      <label>&nbsp;</label>
      <input name="" type="submit" class="btn" value="确定"/>

      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input name="" type="button" class="btn" value="返回" onclick="window.location.href='brandList.html'"/>
    </li>
  </ul>
  </form>
</div>
<script type="text/javascript">
  $(function () {

    let brandList = [];
    axiosJson(
            'get',//提交方法
            '/purchase/brandName',//提交地址
            null,
            null
    ).then((res) => {
      brandList = res.data.resultData
    })
    //获取当前时间
    let s = new Date().toLocaleString();
    s = s.replaceAll("/","-")
    $('#createTime').val(s);

    //获取user
    $('#createId').val(localStorage.getItem('uId'));
    $('#createName').val(localStorage.getItem('username'));

    $('#brandName').blur(check);  //光标验证

    $('#form').submit(function () {
      let brand = $('#form').serializeArray();
      if (check()){
          axiosJson(
                  'get',//提交方法
                  '/purchase/addBrand',//提交地址
                  brand,
                  null
          ).then((res) => {
            location.href="brandList.html";
          })
        }
        return false;
    })



    //表单验证  品牌非空与是否存在验证
    function check() {
      // $("#form").unbind("submit")
      // $('#form').submit(function () {
      //   check();
      //   return false;
      // })
      $('.tips').html("");
      var isSuccess=true;
      let brandName=$('#brandName').val();
      if (brandName==null||brandName==''){
        $('.tips').html("品牌名称不能为空").css("color","red");
        isSuccess=false;
      }else {
        for (let i = 0; i < brandList.length; i++) {
          if (brandName == brandList[i]) {
            isSuccess = false;
            $('.tips').html("品牌名称不能重复").css("color","red");
            break;
          }
        }
      }
      return isSuccess;
    }
  })


</script>
</body>
</html>
